<?php
/**
 * Created by PhpStorm.
 * User: xu42
 * Date: 16-9-8
 * Time: 下午14:25
 */

namespace App\Library;


/**
 * Class Log
 * @package App\Library
 */
class Log
{
    /**
     * 记录 微信用户发送的消息和事件
     * @param $message
     */
    public static function addWechatMessage( $message )
    {
        $modelLogMessages = new \App\Models\LogMessages;

        $modelLogMessages->to_user_name   = $message->ToUserName;
        $modelLogMessages->from_user_name = $message->FromUserName;
        $modelLogMessages->create_time    = $message->CreateTime;
        $modelLogMessages->msg_type       = $message->MsgType;
        $modelLogMessages->msg_id         = $message->MsgId;

        switch ( $message->MsgType ) {
            case 'event':
                $modelLogMessages->msg_id = '';     // event 没有 msg_id
                $modelLogMessages->event  = $message->Event;

                if ( $message->Event == 'CLICK' ) {
                    $modelLogMessages->even_key = $message->EventKey;
                }
                if ( $message->Event == 'SCAN' ) {
                    $modelLogMessages->even_key = $message->EventKey;
                    $modelLogMessages->ticket   = $message->Ticket;
                }
                if ( $message->Event == 'LOCATION' ) {
                    $modelLogMessages->latitude  = $message->Latitude;
                    $modelLogMessages->longitude = $message->Longitude;
                    $modelLogMessages->precision = $message->Precision;
                }
                $modelLogMessages->save();
                break;
            case 'image':
                $modelLogMessages->pic_url = $message->PicUrl;
                $modelLogMessages->save();
                break;
            case 'link':
                $modelLogMessages->title       = $message->Title;
                $modelLogMessages->description = $message->Description;
                $modelLogMessages->url         = $message->Url;
                $modelLogMessages->save();
                break;
            case 'location':
                $modelLogMessages->location_x = $message->Location_X;
                $modelLogMessages->location_y = $message->Location_Y;
                $modelLogMessages->scale      = $message->Scale;
                $modelLogMessages->label      = $message->Label;
                $modelLogMessages->save();
                break;
            case 'shortvideo':
                $modelLogMessages->media_id       = $message->MediaId;
                $modelLogMessages->thumb_media_id = $message->ThumbMediaId;
                $modelLogMessages->save();
                break;
            case 'text':
                $modelLogMessages->content = $message->Content;
                $modelLogMessages->save();
                break;
            case 'video':
                $modelLogMessages->media_id       = $message->MediaId;
                $modelLogMessages->thumb_media_id = $message->ThumbMediaId;
                $modelLogMessages->save();
                break;
            case 'voice':
                $modelLogMessages->media_id    = $message->MediaId;
                $modelLogMessages->format      = $message->Format;
                $modelLogMessages->recognition = $message->Recognition;
                $modelLogMessages->save();
                break;
            default:
                $modelLogMessages->save();
                break;
        }
    }

}